<template>
  <div class="setOilPrice">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="开启设置油价" prop="is_oil">
        <el-switch v-model="form.is_oil" :active-value="1" :inactive-value="0"></el-switch>
      </el-form-item>
      <el-form-item label="油价模式" prop="id_num">
        <el-switch
          :disabled="form.is_oil == 0"
          v-model="form.type"
          :active-value="1"
          :inactive-value="0"
          :active-text="'比例油价'"
          :inactive-text="'固定油价'"
          @change="switchChange"
        ></el-switch>
      </el-form-item>
      <el-form-item label="固定油价" prop="fix_oil_price" v-if="form.type == 0 && form.is_oil == 1">
        <el-input type="number" v-model="form.fix_oil_price" placeholder="输入固定油价"></el-input>
      </el-form-item>
      <el-form-item
        label="比例油价"
        prop="proportion_oil_price"
        v-if="form.type == 1 && form.is_oil == 1"
      >
        <el-select v-model="form.proportion_oil_price" placeholder="请选择比例">
          <el-option v-for="(item,index) in priceList" :key="index" :label="item" :value="item"></el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <div class="dialog-footer">
      <el-button type="default" @click="$emit('close')">取消</el-button>
      <el-button type="primary" @click="sure">设置</el-button>
    </div>
  </div>
</template>

<script>
import { setUpOil } from "@/api/gasStation";
export default {
  name: "setOilPrice",
  props:{
    oilRow:{
      default:{},
      type:Object
    }
  },
  data() {
    var validateFix = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入固定油价!"));
      } else if (value == "0") {
        callback(new Error("固定油价不能为0!"));
      } else {
        callback();
      }
    };
    return {
      priceList: [10, 20, 30, 40, 50],
      form: {
        type: 0,
        is_oil: "0",
        fix_oil_price: "",
        proportion_oil_price: ""
      },
      rules: {
        fix_oil_price: [
          { validator: validateFix, trigger: "blur" }
        ],
        proportion_oil_price: [
          { required: true, message: "请选择比例油价", trigger: "change" }
        ]
      },
      readonly: false
    };
  },
  mounted() {
    this.initial();
  },
  methods: {
    async initial() {
      //设置回显
      if(this.oilRow.fix_oil_price != '0'){
        this.form.fix_oil_price = this.oilRow.fix_oil_price;
        this.form.type = 0;
        this.form.is_oil = 1;
      }
      if(this.oilRow.proportion_oil_price != '0'){
        this.form.proportion_oil_price = this.oilRow.proportion_oil_price;
        this.form.type = 1;
        this.form.is_oil = 1;
      }

    },
    switchChange(val){
      setTimeout(() => {
        this.$refs.form.validateField(val == 0 ? 'fix_oil_price' : 'proportion_oil_price');
      }, 0);
    },
    sure() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          let option = {
            id:this.oilRow.id,
            ...this.form
          }
          if(option.is_oil == 0){
            //没有开启设置油价
            option.fix_oil_price = 0;
            option.proportion_oil_price = 0;
          }else{
            //开启设置油价 
            if(option.type == 0){
              //设置固定油价，比例油价数值清空
              option.proportion_oil_price = 0;
            }else{
              //设置比例油价，固定油价数值清空
              option.fix_oil_price = 0;
            }
          }
          delete option.type;
          let result = await setUpOil(option);
          if(result.code == 200){
            this.$message.success('设置成功');
            this.$emit('refresh');
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.setOilPrice {
  .dialog-footer {
    display: flex;
    justify-content: center;
  }
}
</style>
